package com.cj.allTest.jira;

import com.atlassian.jira.rest.client.api.IssueRestClient;
import com.atlassian.jira.rest.client.api.JiraRestClient;
import com.atlassian.jira.rest.client.api.domain.BasicIssue;
import com.atlassian.jira.rest.client.api.domain.Issue;
import com.atlassian.jira.rest.client.api.domain.input.*;
import com.atlassian.jira.rest.client.internal.async.AsynchronousJiraRestClientFactory;
import com.google.common.collect.Lists;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;

/**
 * Desc:
 * Author: Shi Yueqi
 * Date: 2022/10/21 19:36
 */
public class JiraTest {

    private JiraRestClient jiraRestClient;
    private IssueRestClient issueClient;

    /**
     * 登录JIRA并返回指定的JiraRestClient对象
     * 这里不要每次都new，会造成 ApacheAsyncHttpClient 内存泄漏
     * 用统一的一个client就可以了
     */
    public void initJiraRestClient() {
        AsynchronousJiraRestClientFactory asyncClientFactory = new AsynchronousJiraRestClientFactory();
        jiraRestClient = asyncClientFactory.createWithBasicHttpAuthentication(
                URI.create("http://jira.dop.yunquna.com"), "jira_auto", "fua!ZnAQ.b3S%6TMe0cO"
        );

        issueClient = jiraRestClient.getIssueClient();
    }

    /**
     * 关闭jira client
     */
    public void closeJiraRestClient() {
        try {
            if (jiraRestClient != null) {
                jiraRestClient.close();
            }
        } catch (IOException e) {
            System.out.println("closeJiraRestClient 异常: " + e.getMessage());
        }
    }

    /**
     * 查询需求
     */
    public void getIssue() {
        Issue issue = issueClient.getIssue("VERPOOL-55903").claim();
        System.out.println(issue);
    }


    /**
     * 创建jira需求
     */
    public void createIssue() {
        //projectKey是项目名称
        //issueType为需求类型，10900L是齿轮需求
        //summary是需求描述
        IssueInputBuilder builder = new IssueInputBuilder("VERPOOL", 10900L, "mytest create jira summary");

        //需求描述
        builder.setDescription("test create jira description");
        //优先级
        //10000L是P1， 10001L是P2，10002L是P3，10003L是P4，10100L是P5
        builder.setPriorityId(10000L);
        //来源部门
        ComplexIssueInputFieldValue department = ComplexIssueInputFieldValue.with("value", "客服部");
        List<ComplexIssueInputFieldValue> list2 = Lists.newArrayList();
        list2.add(department);
        builder.setFieldValue("customfield_10111", list2);
        //业务线
        builder.setFieldValue("customfield_10106", ComplexIssueInputFieldValue.with("value", "订单"));
        //排期版本
        builder.setFieldValue("customfield_10413", ComplexIssueInputFieldValue.with("name", "Backlog-订单-需求池"));
        //来源人
        List<String> list = Lists.newArrayList();
        list.add("chenjun");
        builder.setFieldValue("customfield_10200", list);
        //PRD地址
        builder.setFieldValue("customfield_10502", "-");
        builder.setFieldValue("customfield_13700", "需求公告描述");

        BasicIssue basicIssue = issueClient.createIssue(builder.build()).claim();
        System.out.println(basicIssue);
    }

    public static void main(String[] args) throws URISyntaxException {
        JiraTest jiraTest = new JiraTest();
        jiraTest.initJiraRestClient();
//
//        jiraTest.getIssue();

        jiraTest.createIssue();
    }
}

